'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ContractTycontractCtrl
 * @description
 * # ContractTycontractCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('ContractTycontractCtrl', function ($scope, DataService,localStorageService, openApi, Util, $location, $routeParams, FormSubmit) {
    var id = $scope.process_id = $routeParams.id
    $scope.getcontracttype = function () {
      var param = {};
      param.type = "通用合同类型";
      Util.getKeyValue(param, function (data) {
        $scope.contracttypeList = data.data
      })
    }
    $scope.choosepeople = function () {
      localStorageService.set("chooseUsrs", $scope.param.aduitUserIds);
      Util.savaParamForReview($scope.param, id);
      // 里面包含了title
      $location.path("userdefined/choosePerson/")
      // $location.path("chooseuser/index/getPageListByAccount");
    }
    $scope.getusers = function () {
      // type = [自定义流程审批人,通用合同审批人,借物资审批人,还物资审批人] 根据流程不同写死
      // parent = 用户选择类型的值
      var param = {
        type: "通用合同审批人",
        parent: $scope.param.contractType
      }
      DataService.getData(openApi.getlistkeyvalues, param).then(function (data) {
        if (data) {
          var user = data[0];
          $scope.param.aduitUserNames = user && user['key'];
          $scope.param.aduitUserIds = user && user['value'];
        }
      })
    };  

    $scope.submit = function (operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.contractcommon;
      options.param = angular.copy($scope.param, {});
      delete options.param.users
      FormSubmit.submit(options,$scope);
    };
    $scope.validate = function () {
      if (!$scope.param.contractType) {
        Util.Toast("请选择合同类型");
        return;
      }
      if (!$scope.param.contractName) {
        Util.Toast("请填写合同名称");
        return;
      }
      if (!$scope.param.contractPartyName) {
        Util.Toast("请填写签约方名称");
        return;
      }
      if (!$scope.param.contractPartyHead) {
        Util.Toast("请填写签约方负责人");
        return;
      }
      if (!$scope.param.contractAmount) {
        Util.Toast("请填写合同金额");
        return;
      }
      if (!$scope.param.ourSideHead) {
        Util.Toast("请填写我方负责人");
        return;
      }
      if (!$scope.param.mainClause) {
        Util.Toast("请填写主要条款");
        return;
      }
      if ($scope.param.relatedModule && $scope.param.relatedModule !== "无") {
        if (!$scope.param.moduleNo) {
          Util.Toast("请选择模块编号");
          return;
        }
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    }
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        var chooseUsrs = localStorageService.get("chooseUsrs");
        localStorageService.remove("chooseUsrs");
        // 获取选择页面返回的数据并且合并到参数中
        if(chooseUsrs){
            $scope.param.aduitUserNames =chooseUsrs.aduitUserNames||"";
            $scope.param.aduitUserIds = chooseUsrs.aduitUserIds||"";    
        }
      });
      $scope.getcontracttype();
    };
    // 初始化
    init();
  });